﻿using MathNet.Numerics.LinearAlgebra.Double;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;


namespace cubeAdjustment
{
    public partial class Mainwindow : Form
    {
        knownData knownData;
        List<iObserve> observedDatas;
        GetCoordinate approximateCoordinate;

        public Mainwindow()
        { InitializeComponent(); }

        /// <summary>
        /// 进行平差
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 平差ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Form activeChild = this.ActiveMdiChild;
            TextBox dataBox = (TextBox)activeChild.ActiveControl;

            if (dataBox.TextLength < 10)
            {
                MessageBox.Show("请输入数据");
                return;
            }

            var HeadText = dataBox.Lines[0];

            if (HeadText.Contains('H'))
            {
                //未知点, 已知点, 观测值个数
                var Robust = dataBox.Lines[1];
                var Info = dataBox.Lines[2].Split(',');
                var UnknownNum = Convert.ToInt32(Info[0]);
                var Known = Convert.ToInt32(Info[1]);
                var ObserveNum = Convert.ToInt32(Info[2]);

                int[] Start = new int[ObserveNum];
                int[] EndPt = new int[ObserveNum];
                var DeltaH = new DenseVector(ObserveNum);
                var Distance = new DenseVector(ObserveNum);

                //已知点点号
                var KnownPointNmu = dataBox.Lines[3].Split(',');
                //已知点高程
                var KnownHPtr = dataBox.Lines[4].Split(',');

                //初始化观测值数据列表
                for (int j = 0; j < ObserveNum; j++)
                {
                    var line = dataBox.Lines[j + 5].Split(',');
                    Start[j] = Convert.ToInt32(line[0]);
                    EndPt[j] = Convert.ToInt32(line[1]);
                    DeltaH[j] = Convert.ToDouble(line[2]);
                    Distance[j] = Convert.ToDouble(line[3]);
                }

                //初始化已知点数据列表
                //TODO:合并
                int[] KnownPoints = new int[Known];
                double[] KnownH = new double[Known];
                for (int k = 0; k < Known; k++)
                {
                    KnownPoints[k] = Convert.ToInt32(KnownPointNmu[k]);
                    KnownH[k] = Convert.ToDouble(KnownHPtr[k]);
                }

                var H_Pingcha = new ElevationFeature(UnknownNum, Known, KnownPoints, KnownH, Start, EndPt, DeltaH, Distance, Robust);
                Report report = new Report(H_Pingcha);
                Output MDIChild = new Output();
                MDIChild.MdiParent = this;
                MDIChild.Show();
                Form Child = this.ActiveMdiChild;
                TextBox Box = (TextBox)Child.ActiveControl;
                Box.Text = report.GetReport;
                return;
            }

            if (HeadText.Contains('L'))
            {
                observedDatas = new List<iObserve>();

                //读取已知数据
                string[] s;

                int IntPtr = 1;
                try
                {
                    s = dataBox.Lines[IntPtr++].Split(',');
                    knownData = new knownData(Convert.ToDouble(s[0]), Convert.ToDouble(s[1]), Convert.ToDouble(s[2]));
                    while (true)
                    {
                        s = dataBox.Lines[IntPtr++].Split(',');
                        if (s.Length != 3) break;
                        knownData.pointDatas.Add(new iPoint(s[0], Convert.ToDouble(s[1]), Convert.ToDouble(s[2])));
                    }
                    while (true)
                    {
                        iObserve observedData = new iObserve(s[0]);
                        while (true)
                        {
                            s = dataBox.Lines[IntPtr++].Split(',');
                            if (s.Length != 3) break;
                            observedData.originalDatas.Add(new oriData(s[0], s[1], Convert.ToDouble(s[2])));
                        }
                        observedDatas.Add(observedData);
                    }
                }
                catch
                { }

                try
                { approximateCoordinate = new GetCoordinate(knownData, observedDatas); }
                catch
                { MessageBox.Show("计算出错"); }


                try
                {
                    var result = new PlaneFeature(approximateCoordinate, observedDatas, knownData);
                }
                catch
                { MessageBox.Show("计算出错"); }

            }

            Output newMDIChild = new Output();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }


        /// <summary>
        /// 坐标转换工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 坐标变换工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PluginB2X newMDIChild = new PluginB2X();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        /// <summary>
        /// 正反算工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 坐标正反算工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Gauss newMDIChild = new Gauss();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }

        private void 保存工程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Save.Title = "保存文件";
            Save.ShowDialog();
            Form activeChild = this.ActiveMdiChild;
            TextBox dataBox = (TextBox)activeChild.ActiveControl;
            var Str = dataBox.Text;
            StreamWriter sw = File.AppendText(Save.FileName);
            sw.WriteLine(Str);
            sw.Flush();
            sw.Close();
        }

        private void 打开工程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Open.Title = "打开文件";
            Open.ShowDialog();
            string Str = File.ReadAllText(Open.FileName);
            Input newMDIChild = new Input();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
            Form activeChild = this.ActiveMdiChild;
            TextBox dataBox = (TextBox)activeChild.ActiveControl;
            dataBox.Text = Str;
            return;
        }

        private void 新建工程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Input newMDIChild = new Input();
            newMDIChild.MdiParent = this;
            newMDIChild.Show();
        }
    }
}
